package com.ruix.app.util;

import android.text.format.DateUtils;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Created by hadexs on 2017/4/28.
 */

public class DateUtil {
    // 格式化基本方法
    public final static long ONEWEEK = 518400000;// 604800000;// 6*24*60*60*1000;
    public final static long TWODAY = 172800000;// 2*24*60*60*1000;
    public final static long ONEDAY = 86400000;
//    public final static float YEAR = 31536000;


//    public static String format(DateFormatEum formatEum, Date date) {
//        DateFormat format = new SimpleDateFormat(formatEum.getValue());
//        return format.format(date);
//    }

    public static String createMsgTimeStyle(long createTime) {
        long todayStart = DateUtil.getTodayMillis();
        Date date = new Date(createTime);
        date.getYear();
        if (DateUtils.isToday(createTime)) { //
             SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
             return sdf.format(date);
        } else if (createTime >todayStart - ONEDAY) {
             SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
             return "昨天 " + sdf.format(date);
        } else if (createTime >todayStart - TWODAY) {
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
            return "前天 " + sdf.format(date);
        } else {
            Date currentDate = new Date(todayStart);
            if (date.getYear() == currentDate.getYear()){
                SimpleDateFormat sdf = new SimpleDateFormat("M月d日");
                return sdf.format(date);
            } else {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy年M月d日");
                return sdf.format(date);
            }
        }
    }

    public static long getTodayMillis() {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }
    private static String[] cnNumbers = {"零","一","二","三","四","五","六","七","八","九","十","十一","十二"};

    public static String buildChinaTime(long timesatmp){
        if (timesatmp ==0){
            timesatmp = System.currentTimeMillis();
        }
        StringBuffer sb = new StringBuffer();
        Date d1 = new Date(timesatmp);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d1);
        int year = calendar.get(Calendar.YEAR);

        int first = year / 1000; // 2017
        int second = year / 100 %10;
        int three = year / 10 %10;
        int four = year % 10;

        sb.append(cnNumbers[first]);
        sb.append(cnNumbers[second]);
        sb.append(cnNumbers[three]);
        sb.append(cnNumbers[four]);
        sb.append("年");

        int month = calendar.get(Calendar.MONTH) + 1; //0表示1月份
        sb.append(cnNumbers[month]);
        sb.append("月");
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        if (day < 10){
            sb.append(cnNumbers[day]);
        } else if (day < 20){
            sb.append("十");
            int seven = day % 10;
            if (seven >0) {
                sb.append(cnNumbers[seven]);
            }
        } else {
            int sex = day / 10;
            sb.append(cnNumbers[sex]);
            sb.append("十");
            int seven = day % 10;
            if (seven >0) {
                sb.append(cnNumbers[seven]);
            }
        }
        sb.append("日");
        return sb.toString();
    }

    public static String studyDay(long openTime, long closeTime) {
        long interver = closeTime - openTime;
        double uptotalDay =  ((double) interver) / 8640000.0;
        int totalTime = (int) Math.ceil(uptotalDay); //向上取整
        if (totalTime ==0){
            totalTime = 1;  //服务器时间返回为0
        }
        if (totalTime < 10) {
            return cnNumbers[totalTime];
        } else if (totalTime < 100) {
            int last = totalTime % 10;
            int lastt = totalTime / 10;
            return cnNumbers[lastt] + "十" + cnNumbers[last];
        } else if (totalTime < 1000) {
            String ruslt = "";
            int first = totalTime / 100;
            ruslt = cnNumbers[first] + "百";
            int tmp = totalTime % 100;
            if (tmp < 10) {
                ruslt += cnNumbers[tmp];
            } else {
                int last = tmp % 10;
                int lastt = tmp / 10;
                ruslt += cnNumbers[lastt] + "十" + cnNumbers[last];
            }
            return ruslt;
        } else if (totalTime < 10000) {
            String ruslt = "";
            int first = totalTime / 1000;
            ruslt = cnNumbers[first] + "千";
            int tmp = totalTime % 1000;
            ruslt = ruslt +  abc(tmp);
            return ruslt;
        } else {
            return "很多";
        }
    }

    public static String abc(int totalTime){
        if (totalTime < 10) {
            return cnNumbers[totalTime];
        } else if (totalTime < 100) {
            int last = totalTime % 10;
            int lastt = totalTime / 10;
            return cnNumbers[lastt] + "十" + cnNumbers[last];
        } else if (totalTime < 1000) {
            String ruslt = "";
            int first = totalTime / 100;
            ruslt = cnNumbers[first] + "百";
            int tmp = totalTime % 100;
            if (tmp < 10) {
                ruslt += cnNumbers[tmp];
            } else {
                int last = tmp % 10;
                int lastt = tmp / 10;
                ruslt += cnNumbers[lastt] + "十" + cnNumbers[last];
            }
            return ruslt;
        }else {
            return "";
        }

    }

    public static String millisecondToMinute(long millisecond) {
        long second = millisecond / 1000;
        long minute = second / 60;
        long leftSecond = second % 60;
        String minuteStr;
        if (minute < 10) {
            minuteStr = "0"+minute;
        } else {
            minuteStr = String.valueOf(minute);
        }

        String leftSecondStr;
        if (leftSecond < 10) {
            leftSecondStr = "0"+leftSecond;
        } else {
            leftSecondStr = String.valueOf(leftSecond);
        }
        return String.format("%s:%s", minuteStr, leftSecondStr);
    }

}
